﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Framework.Message.SMS.SGIP
{
    public class ByteStringConvert
    {
        /// <summary>
        /// 转换整形数据网络次序的字节数组
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public static byte[] String2Bytes(string s, MessageCoding coding)
        {
            switch (coding)
            {
                case MessageCoding.ASCII:
                    return Encoding.ASCII.GetBytes(s);
                case MessageCoding.WriteCard:
                    return Encoding.Default.GetBytes(s);
                case MessageCoding.Binary:
                    return Encoding.Default.GetBytes(s);
                case MessageCoding.Ucs2:
                    return Encoding.BigEndianUnicode.GetBytes(s);
                case MessageCoding.Gbk:
                    return Encoding.Default.GetBytes(s);
                case MessageCoding.Others:
                    return Encoding.Default.GetBytes(s);
                default:
                    return Encoding.Default.GetBytes(s);
            }
        }

        /// <summary>
        /// 返回字节数组代表的整数数字，4个数组
        /// </summary>
        /// <param name="bs"></param>
        /// <param name="startIndex"></param>
        /// <returns></returns>
        public static string Bytes2String(byte[] bs, int startIndex, int count, MessageCoding coding)
        {
            switch (coding)
            {
                case MessageCoding.ASCII:
                    return Encoding.ASCII.GetString(bs, startIndex, count);
                case MessageCoding.WriteCard:
                    return Encoding.Default.GetString(bs, startIndex, count);
                case MessageCoding.Binary:
                    return Encoding.Default.GetString(bs, startIndex, count);
                case MessageCoding.Ucs2:
                    return Encoding.BigEndianUnicode.GetString(bs, startIndex, count);
                case MessageCoding.Gbk:
                    return Encoding.Default.GetString(bs, startIndex, count);
                case MessageCoding.Others:
                    return Encoding.Default.GetString(bs, startIndex, count);
                default:
                    return Encoding.Default.GetString(bs, startIndex, count);
            }
        }

        /// <summary>
        /// 没有指定起始索引
        /// </summary>
        /// <param name="bs"></param>
        /// <returns></returns>
        public static string Bytes2String(byte[] bytes, MessageCoding coding)
        {
            switch (coding)
            {
                case MessageCoding.ASCII:
                    return Encoding.ASCII.GetString(bytes);
                case MessageCoding.WriteCard:
                    return Encoding.Default.GetString(bytes);
                case MessageCoding.Binary:
                    return Encoding.Default.GetString(bytes);
                case MessageCoding.Ucs2:
                    return Encoding.BigEndianUnicode.GetString(bytes);
                case MessageCoding.Gbk:
                    return Encoding.Default.GetString(bytes);
                case MessageCoding.Others:
                    return Encoding.Default.GetString(bytes);
                default:
                    return Encoding.Default.GetString(bytes);
            }
        }

        public static int GetString2ByteCount(string s, MessageCoding coding)
        {
            switch (coding)
            {
                case MessageCoding.ASCII:
                    return Encoding.ASCII.GetByteCount(s);
                case MessageCoding.WriteCard:
                    return Encoding.Default.GetByteCount(s);
                case MessageCoding.Binary:
                    return Encoding.Default.GetByteCount(s);
                case MessageCoding.Ucs2:
                    return Encoding.BigEndianUnicode.GetByteCount(s);
                case MessageCoding.Gbk:
                    return Encoding.Default.GetByteCount(s);
                case MessageCoding.Others:
                    return Encoding.Default.GetByteCount(s);
                default:
                    return Encoding.Default.GetByteCount(s);
            }
        }

        public static byte[] BytePadLeft(byte[] bytes, int length, byte b)
        {
            byte[] bs;
            if (bytes.Length >= length)
            {
                bs = new byte[bytes.Length];
                bs.CopyTo(bytes, 0);
                return bs;
            }
            bs = new byte[length];
            for (int i = 0; i < length - bytes.Length; i++)
            {
                bs[i] = b;
            }
            bytes.CopyTo(bs, length - bytes.Length);
            return bs;
        }
    }
}
